﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;
using ARR.Entities;
using System.ServiceModel;

namespace ARR.UI.Models
{
    public interface IReportCategoryModel : IModelBase, INotifyPropertyChanged
    {
        List<ImporterResult> Results { get; set; }

        List<Category> Categories { get; set; }

        RangeReview RangeReview { get; set; }
    }


    public class ReportCategoryModel : ModelBase, IReportCategoryModel
    {
        public ReportCategoryModel()
        {
            this.Results = new List<ImporterResult>();
            this.RangeReview = WorkspaceData.Instance.RangeReview;
            this.Categories = new List<Category>();
            LoadCategories();
        }

        public List<ImporterResult> Results { get; set; }

        public List<Category> Categories { get; set; }

        public RangeReview RangeReview { get; set; }

        private void LoadCategories()
        {
            ARR.UI.CategoriesService.CategoriesServiceClient service = new ARR.UI.CategoriesService.CategoriesServiceClient();
            service.GetByLevelsCompleted += new EventHandler<CategoriesService.GetByLevelsCompletedEventArgs>(service_GetByLevelsCompleted);
            service.GetByLevelsAsync(2);
        }

        void service_GetByLevelsCompleted(object sender, CategoriesService.GetByLevelsCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                (sender as ICommunicationObject).Close();

                var categories = e.Result;
                foreach (var category in categories.Where(x => x.Level == 2))
                {
                    category.SubCategories = new List<Category>();
                    category.SubCategories.AddRange(categories.Where(x => x.ParentId == category.Id));
                }
                Categories = categories.Where(x => x.Level == 2).OrderBy(x => x.Name).ToList();
                NotifyPropertyChanged("Categories");
            }
            else
            {
                (sender as ICommunicationObject).Abort();
                throw e.Error;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }


    public class ReportCategoryModelMock : MockModelBase, IReportCategoryModel
    {
        public ReportCategoryModelMock()
        {
            this.Results = new List<ImporterResult>();

            RangeReview = new RangeReview
            {
                Title = "Range Review 1",
                CountryId = 23,
                FirstMonth = new DateTime(2010, 05, 1),
                TotalMonths = 12,
                TargetCurrencyId = 5
            };

            this.Categories = new List<Category>();
        }

        public List<ImporterResult> Results { get; set; }

        public List<Category> Categories { get; set; }

        public RangeReview RangeReview { get; set; }


        public event PropertyChangedEventHandler PropertyChanged;
    }

}
